// *** LICENSE HEADER ***
// Filename: ./numberfield.cpp
// This file is part of Nodewarz 0.1
// Nodewarz is a 3d OpenGL strategy game
// Copyright (C) 2007 CZestmyr
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
// *** END LICENSE HEADER ***
#include "numberfield.h"

numberfield::numberfield(int newId, int posX, int posY, int width, int height, int newFontHeight, int newMaxvalue, const char* text, int maxlength):
    guielement(posX, posY, text, newId){
    //Constraint the text size
    int l = strlen(text);
    if (l >= maxlength+1)
        l = maxlength;

    //Create a buffer for the input text and copy the initial text into it
    textValue = new char[maxlength+1];
    for (int i = 0; i < l; ++i){
        textValue[i] = text[i];
    }
    textValue[l] = 0x00;

    //Free the implicit guielement buffer and set the pointer to the input buffer
    FreeBuffer();
    textBuffer = textValue;

    //Calculate the initial text value
    numvalue = strtol(textValue, NULL, 10);

    //Set the object properties
    w = width; h = height;
    fontHeight = newFontHeight;
    maxvalue = newMaxvalue;
    maxlen = maxlength;
}

numberfield::~numberfield(){}

void numberfield::Deactivate(){
    //Calculate the correct number value upon deactivation
    numvalue = strtol(textValue, NULL, 10);
    //Constraint the value
    Constrainti(numvalue, 0, maxvalue);
    //Update the text buffer to the constrainted value
    sprintf(textValue, "%i", numvalue);
    active = false;
}

int numberfield::GetValue(){
    return numvalue;
}

int numberfield::Click(int clickX, int clickY){
    if (!enabled) return 0;
    if (clickX >= x && clickX <= x + w && clickY >= y && clickY <= y + h)
        return id;
    else
        return 0;
}

int numberfield::Release(int clickX, int clickY){
    return Click(clickX, clickY);
}

void numberfield::KeyPress(SDLKey key, char c){
    int l = strlen(textValue);
    if (key >= SDLK_0 && key <= SDLK_9 && l < maxlen){
        textValue[l] = key;
        textValue[l+1] = 0x00;
        return;
    }
    if (key >= SDLK_KP0 && key <= SDLK_KP9 && l < maxlen){
        textValue[l] = key - SDLK_KP0 + SDLK_0;
        textValue[l+1] = 0x00;
        return;
    }
    if (key == SDLK_BACKSPACE && l > 0){
        textValue[l-1] = 0x00;
        return;
    }
    if (key == SDLK_RETURN || key == SDLK_KP_ENTER || key == SDLK_ESCAPE){
        Deactivate();
        return;
    }
}

void numberfield::Draw(fonts* fontDrawer){
    glDisable(GL_BLEND);
    glDisable(GL_DEPTH_TEST);
    glDisable(GL_TEXTURE_2D);

    //Draw the border
    glBegin(GL_LINE_STRIP);
    if (active)
        glColor3ub(255,80,80);
    else
        glColor3ub(80,80,80);
    glVertex2i(x, _HEIGHT-y);
    glVertex2i(x+w, _HEIGHT-y);
    glVertex2i(x+w, _HEIGHT-y-h);
    glVertex2i(x, _HEIGHT-y-h);
    glVertex2i(x, _HEIGHT-y);
    glEnd();

    //Print the text
    fontDrawer->Print(FT_MENU, textValue, x + 5, _HEIGHT - y - h/2 - fontHeight/2+2, fontHeight, button::textColor);
}
